package com.flnet.web.controller.system;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.flnet.common.annotation.Log;
import com.flnet.common.enums.BusinessType;
import com.flnet.system.domain.Region;
import com.flnet.system.service.IRegionService;
import com.flnet.framework.web.base.BaseController;
import com.flnet.common.page.TableDataInfo;
import com.flnet.common.base.AjaxResult;
import com.flnet.common.utils.poi.ExcelUtil;

/**
 * 区域 信息操作处理
 * 
 * @author flnet
 * @date 2019-03-22
 */
@Controller
@RequestMapping("/system/region")
public class RegionController extends BaseController
{
    private String prefix = "system/region";
	
	@Autowired
	private IRegionService regionService;
	
	@RequiresPermissions("system:region:view")
	@GetMapping()
	public String region()
	{
	    return prefix + "/region";
	}
	
	/**
	 * 查询区域列表
	 */
	@RequiresPermissions("system:region:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(Region region)
	{
		startPage();
        List<Region> list = regionService.selectRegionList(region);
		return getDataTable(list);
	}
	
	
	/**
	 * 导出区域列表
	 */
	@RequiresPermissions("system:region:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Region region)
    {
    	List<Region> list = regionService.selectRegionList(region);
        ExcelUtil<Region> util = new ExcelUtil<Region>(Region.class);
        return util.exportExcel(list, "region");
    }
	
	/**
	 * 新增区域
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存区域
	 */
	@RequiresPermissions("system:region:add")
	@Log(title = "区域", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(Region region)
	{		
		return toAjax(regionService.insertRegion(region));
	}

	/**
	 * 修改区域
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Integer id, ModelMap mmap)
	{
		Region region = regionService.selectRegionById(id);
		Region region1 = new Region();
		region1.setType(region.getType());
		List<Region> regionList = regionService.selectParentRegionList(region1);
		mmap.put("region", region);
		mmap.put("regionList", regionList);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存区域
	 */
	@RequiresPermissions("system:region:edit")
	@Log(title = "区域", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(Region region)
	{		
		return toAjax(regionService.updateRegion(region));
	}
	
	/**
	 * 删除区域
	 */
	@RequiresPermissions("system:region:remove")
	@Log(title = "区域", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(regionService.deleteRegionByIds(ids));
	}

	@ResponseBody
	@RequestMapping("/updateParentRegion")
	public AjaxResult updateParentRegion(int type){
		Region item = new Region();
		item.setType(type);
		List<Region> regionList = regionService.selectParentRegionList(item);
		return AjaxResult.success().put("regionList",regionList);
	}
}
